/*
    * Copyright (c) 2024 Hunan OpenValley Digital Industry Development Co., Ltd.
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    *     http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
 */

#ifndef NAPI_FILE_SHARE_UTIL_H
#define NAPI_FILE_SHARE_UTIL_H

#include <map>
#include <memory>
#include <mutex>
#include <string>

#include "dm_device_info.h"
#include "dm_publish_info.h"
#include "napi/native_api.h"
#include "napi/native_common.h"
#include "napi/native_node_api.h"
#include "nlohmann/json.hpp"
#include "file_share_log.h"
#include "file_share_common.h"

namespace OHOS {
namespace FileShare {
enum BussinessErrorCode {
    // construct sharemanager failed.
    ERR_CONSTRUCT_FAILED = 101,
    // Permission verify failed.
    // The caller is not a system application.
    ERR_NO_PERMISSION = 201,
    ERR_NOT_SYSTEM_APP = 202,
    // Input parameter error.
    ERR_INVALID_PARAMS = 401,
    // Capability not supported.
    ERR_CAPABILITY_NOT_SUPPORT = 801,
    // Failed to obtain the service.
    DM_ERR_OBTAIN_SERVICE = 11600102,
    // Authentication invalid.
    DM_ERR_AUTHENTICALTION_INVALID = 11600103,
    // Discovery invalid.
    DM_ERR_DISCOVERY_INVALID = 11600104,
    // Publish invalid.
    DM_ERR_PUBLISH_INVALID = 11600105,
    // Failed to operate the function.
    ERR_OPERATION_FAILED = 2501000,
};

void JsToDmPublishInfo(const napi_env &env, const napi_value &object,
                        OHOS::DistributedHardware::DmPublishInfo &info);
void JsToDmDiscoveryExtra(const napi_env &env, const napi_value &object, std::string &extra);
bool JsToDiscoverTargetType(napi_env env, const napi_value &object, int32_t &discoverTargetType);
void DmDeviceBasicToJsObject(napi_env env, const OHOS::DistributedHardware::DmDeviceBasicInfo &vecDevInfo, napi_value &result);
bool CheckArgsCount(napi_env env, bool assertion, const std::string &message);
bool CheckArgsType(napi_env env, bool assertion, const std::string &paramName, const std::string &type);
napi_value CreateBusinessError(napi_env env, int32_t errCode, bool isAsync = true);
bool IsSystemApp();
void SetValueInt32(const napi_env &env, const std::string &fieldStr, const int32_t intValue,
                   napi_value &result);
void SetValueUtf8String(const napi_env &env, const std::string &fieldStr, const std::string &str,
                        napi_value &result);
void SetValueBoolean(const napi_env &env, const std::string &fieldStr, const bool boolValue, napi_value &result);
bool JsToStringAndCheck(napi_env env, napi_value value, const std::string &valueName, std::string &strValue);   
void JsToBindParam(const napi_env &env, const napi_value &object, std::string &bindParam, int32_t &bindType);
bool IsFunctionType(napi_env env, napi_value value);
bool IsJSObjectType(napi_env env, napi_value value, const std::string &param);
void JsToFileInfo(const napi_env &env, const napi_value &object, FSNapiFileInfo &fileInfo);
bool SendInfoToJsObject(napi_env env, const SendInfo &info, napi_value &result);
void FileInfoToJsObject(napi_env env, const FileInfo &fileInfo, napi_value &result);
bool FileInfoToToJsArray(const napi_env &env, const std::vector<FileInfo> &vecFileInfo, const int32_t idx, napi_value &arrayResult);
bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType);
bool GetStringFromJS(napi_env env, napi_value argv, std::string &jsString);
bool GetNumberFromJS(napi_env env, napi_value args, int32_t &param);
bool IsNaN(napi_env env, napi_value args);
void DeviceBasicInfoToJsArray(const napi_env &env, const std::vector<OHOS::DistributedHardware::DmDeviceBasicInfo> &vecDevInfo, 
                              const int32_t idx, napi_value &arrayResult);
#define ASSERT_AND_THROW_PARAM_ERROR(env, assertion, errMsg)               \
    do {                                                                       \
        if (!(assertion)) {                                                    \
            napi_throw_error(env, std::to_string(ERR_INVALID_PARAMS).c_str(),errMsg);         \
            napi_value ret = nullptr;                                      \
            return ret;                                                    \
        }                                                                    \
    } while (0)
} // namespace FileShare
} // namespace OHOS
#endif // NAPI_FILE_SHARE_UTIL_H